// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import Util from './libs/util'
import VueRouter from 'vue-router'
import Routers from './router'
import store from './vuex'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.config.productionTip = false

const RouterConfig = {
  mode: 'history',
  routes: Routers
}
const router = new VueRouter(RouterConfig)

router.beforeEach((to, from, next) => {
  Util.title(to.meta.title)
  if (to.meta.requiresAuth) {
    if (Util.checkSignStatus()) {
      next()
    } else {
      next({
        path: '/signin',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
